package com.hospes.rmes.core.databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.hospes.rmes.models.News;
import com.hospes.rmes.utils.Utils;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

public class DataBaseManager {
	private SQLiteDatabase sqLiteDatabase;
	private DBHendler DBHendler;
	private Context context;

	public DataBaseManager(Context context) {
		this.context = context;
	}

	public SQLiteDatabase openDB() {
		DBHendler = new DBHendler(context);
		sqLiteDatabase = DBHendler.getWritableDatabase();
		return sqLiteDatabase;
	}

	public void closeDB() {
		sqLiteDatabase.close();
	}

//	public void insertData(News news) {
//		ContentValues contentValues = new ContentValues();
//		contentValues.put(DatabaseField.WEB_URL, url);
//		contentValues.put(DatabaseField.WEB_DESCRIPTION, description);
//		contentValues.put(DatabaseField.WEB_TIME, System.nanoTime());
//		contentValues.put(DatabaseField.WEB_SCREENSHOT, image);
//
//		sqLiteDatabase.insert(DatabaseField.WEB_TABLE, null, contentValues);
//
//		ArrayList<SpeedDial> speedDials = getSpeedDialsFromDB();
//		SpeedDialsManager.getInstance().setSpeedDials(speedDials);
//		SpeedDialsManager.getInstance().getSpeedDialsAdapter().notifyDataSetChanged();
//	}

	public void insertData(ArrayList<News> newses) {
		sqLiteDatabase.delete(DatabaseField.TABLE_NEWS, null, null);

		ContentValues contentValues = new ContentValues();
		for (News news : newses) {
			contentValues.put(DatabaseField.TABLE_NEWS_DATE, news.getDate().toLocaleString());
			contentValues.put(DatabaseField.TABLE_NEWS_TITLE, news.getTitle());
			contentValues.put(DatabaseField.TABLE_NEWS_TEXT, news.getText());
			contentValues.put(DatabaseField.TABLE_NEWS_TEASER, news.getTeaser());
			contentValues.put(DatabaseField.TABLE_NEWS_FILES, news.getFiles());
			sqLiteDatabase.insert(DatabaseField.TABLE_NEWS, null, contentValues);
		}
	}

	public ArrayList<News> getNewsListFromDB() {
		ArrayList<News> speedDials = new ArrayList<News>();
		Cursor cursor = sqLiteDatabase.query(DatabaseField.TABLE_NEWS, null, null, null, null, null, null);
		int indexDate = cursor.getColumnIndex(DatabaseField.TABLE_NEWS_DATE);
		int indexTitle = cursor.getColumnIndex(DatabaseField.TABLE_NEWS_TITLE);
		int indexText = cursor.getColumnIndex(DatabaseField.TABLE_NEWS_TEXT);
		int indexTeaser = cursor.getColumnIndex(DatabaseField.TABLE_NEWS_TEASER);
		int indexFiles = cursor.getColumnIndex(DatabaseField.TABLE_NEWS_FILES);

		while (cursor.moveToNext()) {
			News temp = new News(Utils.FromStringToDate(cursor.getString(indexDate)), cursor.getString(indexTitle), cursor.getString(indexFiles), cursor.getString(indexText), cursor.getString(indexTeaser));
			speedDials.add(temp);
		}

		return speedDials;
	}

	private long getRowCount() {
		return sqLiteDatabase.compileStatement("SELECT id FROM " + DatabaseField.TABLE_NEWS).simpleQueryForLong();
	}
}
